package com.eagle.roomsearch.controller.webSocketController;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {

    //這是一個標籤 就是一個頁面
    // 範圍一個就是一個人上線   todo http://localhost:8081/indexUserNumber
    @RequestMapping(value = "/indexUserNumber")
    public String index(Model model) {

        //保持唯一性
        String userNumber = UUID.randomUUID().toString().replace("-", "");

        model.addAttribute("userNumber", userNumber);

        return "index";
    }

    @GetMapping(value = "test1/{test1}/test2/{test2}")
    public Map<String, Object> indexTest(@PathVariable(value = "test1") Object test1, @PathVariable(value = "test2") Object test2) {

        Map<String, Object> result = new HashMap<>();
        result.put("test1", test1);
        result.put("test2", test2);

        return result;
    }

    @Resource(name = "objectRedisTemplate")
    RedisTemplate redisTemplate;

    @GetMapping("redis")
    @ResponseBody
    public Map<String, String> redis() {
        Map<String, String> result = new HashMap();
        result.put("number", "201341404154");
        result.put("name", "陶嘉伟");

        redisTemplate.opsForValue().set("201341404154", result);

        return (Map<String, String>) redisTemplate.opsForValue().get("201341404154");
    }


    @GetMapping("domain")
    public String domain(@RequestParam("domain") String domain, HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.sendRedirect("http://www." + domain + ".iecho.work/Login/CLogin");
        return "";
    }

    @GetMapping("callback")
    @ResponseBody
    public String callback() {

        return "这是重定向页面";
    }
}
